package cn.com.headfree.sysmenu.dao.impl;

import cn.com.headfree.sysmenu.dao.IRoleMenuDao;
import cn.com.headfree.sysmenu.entity.TSysRoleMenu;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("roleMenuDao")
public class RoleMenuDaoImpl extends BaseDaoImpl<TSysRoleMenu> implements IRoleMenuDao {
    private static final Logger LOG = Logger.getLogger(RoleMenuDaoImpl.class);

    @Override
    public List<TSysRoleMenu> checkMenuExistRole(String menuId){
        String sql = "  select rm.id,rm.role_id,rm.menu_id " +
                " from t_sys_role_menu rm " +
                " where 1=1 " ;
        StringBuilder sqlBuilder = new StringBuilder(sql);

        ArrayList<Object> arrayList = new ArrayList<Object>();
        //菜单id
        if (!StringUtil.isNullOrEmpty(menuId)) {
            sqlBuilder.append(" AND rm.menu_id = ? ");
            arrayList.add(menuId);
        }
        return this.findAllBySQL(TSysRoleMenu.class, sqlBuilder.toString(), null,false, arrayList.toArray());
    }

    //通过角色ID删除角色和菜单关联
    @Override
    public void deleteRoleMenuByRoleId(String roleId) {
        String sql = "delete from t_sys_role_menu ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" where 1=1 ");
        Map<String, Object> params = new HashMap<String, Object>();
        sqlBuilder.append(" and role_id = #{role_id,jdbcType=VARCHAR} ");
        params.put("role_id", roleId);
        this.executeSql(sqlBuilder.toString(), params);
    }

    //批量删除角色菜单关联信息
    @Override
    public void deleteRoleMenu(String[] roleIds) {
        String sql = "delete from t_sys_role_menu ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        sqlBuilder.append(" where 1=1 ");
        Map<String, Object> params = new HashMap<String, Object>();
        sqlBuilder.append(" and role_id in ('").append(String.join("','",roleIds)).append("') ");
        this.executeSql(sqlBuilder.toString(), params);
    }

    //批量新增角色菜单信息
    @Override
    public void batchRoleMenu(String values) {
        String sql = "insert into t_sys_role_menu(id, role_id, menu_id) values ";
        StringBuilder sqlBuilder = new StringBuilder(sql);
        Map<String, Object> params = new HashMap<String, Object>();
        sqlBuilder.append(values);
        this.executeSql(sqlBuilder.toString(), params);
    }
}
